<?php
class PostController extends MController {
    const AUTHORIZED_TAGS = '<a><abbr><address><article><audio><b><blockquote><br><cite><code><dd><details><dfn><div><dl><dt><em><footer><h1><h2><h3><h4><h5><h6><header><hgroup><hr><i><img><ins><kbd><li><mark><menu><nav><ol><p><pre><progress><q><samp><section><small><span><strong><sub><sup><time><ul><video>';

    public function indexAction(){
        
    }

    public function createAction(){
        if ($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            $postContent = trim(strip_tags($postData['postContent'], self::AUTHORIZED_TAGS));
            if (!empty($postContent)){
                // Searching for any tags
                $contentArray = explode(' ',$postContent);
                $tagArray = array();
                foreach ($contentArray as $word){
                    if (substr($word,0,1) == '#'){
                        $foundTag = $word;
                        if (!Tag::exists(array('tag'=>$foundTag))){
                            $tag = new Tag();
                            $tag->tag = $foundTag;
                            $tag->save();
                            array_push($tagArray, $tag);
                        } else {
                            $tag = Tag::find(array('tag'=>$foundTag));
                            array_push($tagArray, $tag);
                        }
                    }
                }

                // Creating the new post
                $post = new Post();
                $post->html_content = $postContent;
                $post->date = date('Y-m-d H:i:s');
                // TODO : Put the connected user id
                if (MSession::getInstance()->isLogged()){
                    $profile = Profile::find(array('user_id'=>  MSession::getInstance()->getUserId()));
                    if ($profile !== NULL){
                        $post->profile_id = $profile->id;
                    } else {
                        $post->profile_id = 1;
                    }
                } else {
                    $post->profile_id = 1;
                }
                $post->save();

                foreach ($tagArray as $tag){
                    $postTag = new PostTag();
                    $postTag->post_id = $post->id;
                    $postTag->tag_id = $tag->id;
                    $postTag->save();
                }
            }
            __redirect(__route('ml-microblog'));
        } else {
            $form = new CreatePostForm();
            if ($this->getRequest()->isAjax()){
                $this->renderPartial('create', array('form'=>$form));
            } else {
                $this->render('create', array('form'=>$form));
            }
        }
    }

    public function loadAction(){
        $getParams = $this->getRequest()->getGet();
        $limit = 10;
        $currentPage = (isset($getParams['p']))?$getParams['p']:1;
        $offset = ($currentPage - 1)*$limit;
        
        $nbPosts = Post::count();
        $posts = Post::all(array('order'=>'date DESC', 'limit'=>$limit, 'offset'=>$offset));

        $nbPages = ceil($nbPosts/$limit);
        
        $pager = new MHtmlLinkPager(__route('ml-microblog/post/load'), $nbPages, $currentPage);
        
        $this->renderPartial('load', array('posts'=>$posts, 'pager'=>$pager));
    }

    public function showAction(){
        if ($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            $postId = $postData['id'];
            if (!empty($postId)){
                $post = Post::find(intval($postId));
            }
            $this->renderPartial('show', array('post'=>$post));
        }
    }

    public function searchAction(){
        if ($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            $searchQuery = $postData['q'];
            if (!empty($searchQuery)){
                $posts = Post::find('all', array('conditions'=>'html_content LIKE "%'.$searchQuery.'%"', 'order'=>'date DESC'));
            }
            $this->render('search', array('posts'=>$posts));
        }
    }
}
?>
